use aspect_codec::{Codec, CodecError};
use aspect_msgqueue::QueueIdent;

/// Container for a queue processor state as persisted to disk.
#[derive(Clone)]
pub struct ProcStateContainer {
    data: Vec<u8>,
}

impl ProcStateContainer {
    pub fn new(data: Vec<u8>) -> Self {
        Self { data }
    }

    pub fn from_inst(inst: &impl Codec) -> Result<Self, CodecError> {
        let data = aspect_codec::encode_to_vec(inst)?;
        Ok(Self::new(data))
    }

    pub fn try_parse<C: Codec>(&self) -> Result<C, CodecError> {
        aspect_codec::decode(&self.data)
    }
}

/// Description of a processor job.
#[derive(Copy, Clone, Debug)]
pub struct ProcJob {
    /// The queue ident.
    id: QueueIdent,

    /// Try to process messages up to this index.
    last_msg_idx: u64,
}

impl ProcJob {
    pub fn new(id: QueueIdent, last_msg_idx: u64) -> Self {
        Self { id, last_msg_idx }
    }

    pub fn id(&self) -> &QueueIdent {
        &self.id
    }

    pub fn last_msg_idx(&self) -> u64 {
        self.last_msg_idx
    }
}
